from api.student.models import StudentModel
from api.student.models import StudentModel
from api.utils.serilizers import StudentsSerializer
from api.utils.paginations import MyPagination
import os
from logger.logger import logger_handle
from students_management.settings import BASE_DIR

path = os.sep.join([BASE_DIR, 'logger', 'students.log'])
logger = logger_handle(path)

def process_get(self, request):
    """get请求业务处理"""
    student_query = StudentModel.objects.all()
    pg = MyPagination()
    pager_student = pg.paginate_queryset(queryset=student_query, request=request, view=self)
    ser = StudentsSerializer(instance=pager_student, many=True)
    ret = ser.data
    return ret


def process_post(data):
    """post请求业务处理"""
    username = data.get('username')
    age = data.get('age')
    gender = data.get('gender')
    birthday = data.get('birthday')
    phone = data.get('phone')
    address = data.get('address')
    graduated = data.get('graduated')
    remarks = data.get('remarks')
    if not all([username, age, gender, birthday, phone, address, graduated, remarks]):
        return False  # 参数不全
    StudentModel.objects.create(username=username, age=age, gender=gender, birthday=birthday, phone=phone,
                                address=address, graduated=graduated, remarks=remarks)
    return True


def process_put(request, data):
    """put请求业务处理"""
    pk = request.query_params.get("pk")
    username = data.get('username')
    age = data.get('age')
    gender = data.get('gender')
    birthday = data.get('birthday')
    phone = data.get('phone')
    address = data.get('address')
    graduated = data.get('graduated')
    remarks = data.get('remarks')
    if not all([username, age, gender, birthday, phone, address, graduated, remarks]):
        logger.info("students process_put请求参数不全")
        return False  # 参数不全
    StudentModel.objects.filter(id=pk).update(username=username, age=age, gender=gender, birthday=birthday, phone=phone,
                                              address=address, graduated=graduated, remarks=remarks)
    return True


def process_delete(request):
    """put请求业务处理"""
    pk = request.query_params.get("pk")
    data = StudentModel.objects.filter(id=pk).first()
    if data:
        data.delete()
        return True
    else:
        return False
